package com.example.opengl_fbo.filter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLUtils;

import com.example.opengl_fbo.utils.BufferUtil;
import com.example.opengl_fbo.utils.LogUtil;
import com.example.opengl_fbo.utils.ShaderHelper;
import com.example.opengl_fbo.utils.TextureHelper;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glUniform1i;
import static android.opengl.GLES20.glViewport;

public  class Image2DFilter {



    // 从 res中的raw中读取shader
    public Image2DFilter(Context context , int  vertexShaderRawId, int  fragmentShaderRawId){
        String vertexShaderString = ShaderHelper.readTextFileFromResourceRaw(context ,vertexShaderRawId );
        String fragmentShaderString = ShaderHelper.readTextFileFromResourceRaw(context ,fragmentShaderRawId );
        program = ShaderHelper.buildProgram(vertexShaderString, fragmentShaderString);
        initUniforms();

    }
    // 从assets中的raw中读取shader
    public Image2DFilter(Context context , String vertexShaderAssetsPath, String fragmentShaderAssetsPath){
        String vertexShaderString = ShaderHelper.readTextFileFromResourceAssets(context ,vertexShaderAssetsPath );
        String fragmentShaderString = ShaderHelper.readTextFileFromResourceAssets(context ,fragmentShaderAssetsPath );
        program = ShaderHelper.buildProgram(vertexShaderString, fragmentShaderString);
        initUniforms();

    }
    // 默认设置 filter
    public Image2DFilter(Context context){
        String vertexShaderString = ShaderHelper.readTextFileFromResourceAssets(context ,"lammyFilter/no_filter_vertex_shader.glsl" );
        String fragmentShaderString = ShaderHelper.readTextFileFromResourceAssets(context ,"lammyFilter/no_filter_fragment_shader.glsl" );
        program = ShaderHelper.buildProgram(vertexShaderString, fragmentShaderString);
        initUniforms();
    }

    /**
     * 程序句柄
     */
    protected int program;

    public int getPrgram(){
        return program;
    }

    //顶点坐标
    private float pos[] = {
            -1.0f,  1.0f,
            -1.0f, -1.0f,
            1.0f, 1.0f,
            1.0f,  -1.0f,
    };
    public float coor[] = {
            0.0f , 0.0f,
            0.0f , 1.0f,
            1.0f , 0.0f,
            1.0f , 1.0f
    };

    private int vPositionLocation;
    private int vTextureCoordinateLocation;
    private int vTextureLocation;


    private void initUniforms() {
        vPositionLocation = glGetAttribLocation(program , "vPosition");
        vTextureCoordinateLocation = glGetAttribLocation(program , "inputTextureCoordinate");
        vTextureLocation = GLES20.glGetUniformLocation(program, "vTexture");

        createImageTexture();

    }
    protected void setUniforms() {
        GLES20.glVertexAttribPointer(vPositionLocation, 2, GLES20.GL_FLOAT, false, 8, vPositionBuffer);
        GLES20.glVertexAttribPointer(vTextureCoordinateLocation, 2, GLES20.GL_FLOAT, false, 8,vTextureCoordinateBuffer );
    }

    public void onSizeChanged(int width, int height) {
        if(this.width!=width||this.height!=height){
            this.width=width;
            this.height=height;
            LogUtil.e("height = " + height);
            //创建FrameBuffer和Texture
//            deleteFrameBuffer();
//            GLES20.glGenFramebuffers(1, fFrame, 0);
//            TextureHelper.genTexturesWithParameter(1, outTextureId,0, GLES20.GL_RGBA,width,height);
        }
    }


    /**
     * 启用顶点坐标和纹理坐标进行绘制
     */
    int index= 0;
    public void draw(){
        LogUtil.e("camera filter draw !");
        boolean a= GLES20.glIsEnabled(GLES20.GL_DEPTH_TEST);
        if(a){
            GLES20.glDisable(GLES20.GL_DEPTH_TEST);
        }

        if(mSurfaceTexture!=null){
        //    mSurfaceTexture.updateTexImage();

        }

        OnDraw();

        if(a){
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        }


    }

    private void OnDraw(){
        onClear();
        onUseProgram();
        glViewport(0,0,width , height);
        setUniforms();
        onBindTexture();
        glEnableVertexAttribArray(vPositionLocation);
        glEnableVertexAttribArray(vTextureCoordinateLocation);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP,0,4);
        GLES20.glDisableVertexAttribArray(vPositionLocation);
        GLES20.glDisableVertexAttribArray(vTextureCoordinateLocation);
    }

    private FloatBuffer vPositionBuffer =  BufferUtil.floatToBuffer(pos);
    private FloatBuffer vTextureCoordinateBuffer=  BufferUtil.floatToBuffer(coor);
  //  private float[] pointsMatrix = get90RotationMatrix();// MatrixUtils.getOriginalMatrix();
    private int width , height;
    private SurfaceTexture mSurfaceTexture;


    /**
     * 绑定默认纹理
     */
    private int fFrame[] = new int[1];
    private int textureId[] = new int[1];
    private int outTextureId[] = new int[1];



    protected void onUseProgram(){
        GLES20.glUseProgram(program);
    }

    protected void onBindTexture(){

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0+0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureId[0]);
        ByteBuffer mBuffer = ByteBuffer.allocate(width * height * 4);
        GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, mBuffer);
//        bitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
//        bitmap.copyPixelsFromBuffer(mBuffer);
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,width,height,0,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,mBuffer);
//        if(bitmap!=null&&!bitmap.isRecycled()){
//            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,bitmap,0);
//            bitmap.recycle();
//        }
        GLES20.glUniform1i(vTextureLocation,0);
    }

    /**
     * 清除画布
     */
    protected void onClear(){
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    }


    public  static Bitmap bitmap;
    private void createImageTexture(){

        textureId[0] = TextureHelper.loadTexture(bitmap);
    }



    private void deleteFrameBuffer() {
        GLES20.glDeleteFramebuffers(1, fFrame, 0);
        GLES20.glDeleteTextures(1, outTextureId, 0);
    }



}
